package br.gov.mi.infraestrutura.crud;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

public class CrudServicoImpl<T, ID extends Serializable, R extends JpaRepository<T, ID>>
		implements CrudServico<T, ID> {

	protected R repositorio;

	/**
	 * @param repository
	 *            the repository to set
	 */
	public void setRepositorio(R repositorio) {
		this.repositorio = repositorio;
	}

	@Override
	@Transactional
	public T incluir(T resource) {
		return this.repositorio.save(resource);
	}

	@Override
	@Transactional
	public T alterar(T resource) {
		return this.repositorio.save(resource);
	}

	@Override
	@Transactional
	public void excluir(T resource) {
		this.repositorio.delete(resource);
	}

	@Override
	@Transactional
	public void excluir(ID id) {
		this.repositorio.delete(id);

	}

	@Override
	public T recuperarPorId(ID id) {
		return this.repositorio.findOne(id);
	}

	@Override
	public List<T> recuperarTodos() {
		return this.repositorio.findAll();
	}

	@Override
	public Page<T> recuperarTodos(Pageable pageRequest) {
		return this.repositorio.findAll(pageRequest);
	}

	@Override
	public Long contar() {
		return this.repositorio.count();
	}

}


